<template>
  <div class="grade-analysis">
    <!-- 顶部统计卡片 -->
    <el-row :gutter="20" class="stat-cards">
      <el-col :span="6" v-for="(stat, index) in statistics" :key="index">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-value">{{ stat.value }}</div>
            <div class="stat-label">{{ stat.label }}</div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 图表区域 -->
    <el-row :gutter="20" class="chart-row">
      <el-col :span="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>成绩分布</span>
              <el-select v-model="selectedExam" placeholder="选择考试" size="small">
                <el-option
                  v-for="exam in examList"
                  :key="exam.id"
                  :label="exam.name"
                  :value="exam.id"
                />
              </el-select>
            </div>
          </template>
          <div ref="scoreDistChartRef" style="height: 300px"></div>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>题型得分分析</span>
            </div>
          </template>
          <div ref="questionTypeChartRef" style="height: 300px"></div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 成绩明细表格 -->
    <el-card class="grade-table">
      <template #header>
        <div class="card-header">
          <span>成绩明细</span>
          <div class="header-actions">
            <el-button type="primary" @click="exportGrades">导出成绩</el-button>
            <el-button type="success" @click="generateReport">生成分析报告</el-button>
          </div>
        </div>
      </template>

      <el-table :data="gradeList" style="width: 100%">
        <el-table-column prop="studentId" label="学号" width="120" />
        <el-table-column prop="studentName" label="姓名" width="120" />
        <el-table-column prop="totalScore" label="总分" width="100" />
        <el-table-column prop="choiceScore" label="选择题" width="100" />
        <el-table-column prop="fillScore" label="填空题" width="100" />
        <el-table-column prop="programScore" label="编程题" width="100" />
        <el-table-column prop="rank" label="排名" width="100" />
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="scope">
            <el-button 
              link 
              type="primary" 
              @click="viewDetail(scope.row)"
            >
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted, watch, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import * as echarts from 'echarts'

const router = useRouter()

// 统计数据
const statistics = ref([
  { label: '平均分', value: '85.5' },
  { label: '最高分', value: '98' },
  { label: '最低分', value: '65' },
  { label: '及格率', value: '92%' }
])

// 考试列表
const examList = ref([
  { id: 1, name: 'C语言期中考试' },
  { id: 2, name: 'C语言章节测试' }
])

// 成绩列表
const gradeList = ref([
  {
    studentId: '2024001',
    studentName: '张三',
    totalScore: 92,
    choiceScore: 30,
    fillScore: 22,
    programScore: 40,
    rank: 1
  },
  {
    studentId: '2024002',
    studentName: '李四',
    totalScore: 88,
    choiceScore: 28,
    fillScore: 20,
    programScore: 40,
    rank: 2
  }
])

// 分页
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 选中的考试
const selectedExam = ref('')

// 图表实例
const scoreDistChart = ref(null)
const questionTypeChart = ref(null)
const scoreDistChartRef = ref(null)
const questionTypeChartRef = ref(null)

// 初始化成绩分布图表
const initScoreDistChart = () => {
  if (scoreDistChartRef.value) {
    scoreDistChart.value = echarts.init(scoreDistChartRef.value)
    const option = {
      title: { 
        text: '成绩分布',
        left: 'center'
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow'
        }
      },
      xAxis: {
        type: 'category',
        data: ['<60', '60-70', '70-80', '80-90', '90-100']
      },
      yAxis: {
        type: 'value',
        name: '人数'
      },
      series: [{
        name: '学生人数',
        data: [2, 8, 25, 35, 30],
        type: 'bar',
        barWidth: '40%',
        itemStyle: {
          color: '#409EFF'
        }
      }]
    }
    scoreDistChart.value.setOption(option)
  }
}

// 初始化题型得分分析图表
const initQuestionTypeChart = () => {
  if (questionTypeChartRef.value) {
    questionTypeChart.value = echarts.init(questionTypeChartRef.value)
    const option = {
      title: { 
        text: '题型得分率',
        left: 'center'
      },
      tooltip: {
        trigger: 'item'
      },
      legend: {
        orient: 'vertical',
        left: 'left',
        data: ['平均得分率']
      },
      radar: {
        shape: 'circle',
        indicator: [
          { name: '选择题', max: 100 },
          { name: '填空题', max: 100 },
          { name: '编程题', max: 100 }
        ]
      },
      series: [{
        name: '得分分析',
        type: 'radar',
        data: [{
          value: [85, 78, 82],
          name: '平均得分率',
          areaStyle: {
            color: 'rgba(64, 158, 255, 0.3)'
          },
          lineStyle: {
            color: '#409EFF'
          },
          itemStyle: {
            color: '#409EFF'
          }
        }]
      }]
    }
    questionTypeChart.value.setOption(option)
  }
}

// 监听考试选择变化
watch(selectedExam, (newVal) => {
  if (newVal) {
    // 更新图表和表格数据
    // 这里添加数据加载逻辑
  }
})

// 导出成绩
const exportGrades = () => {
  ElMessage.success('成绩导出成功')
}

// 生成分析报告
const generateReport = () => {
  ElMessage.success('分析报告生成成功')
}

// 查看详情
const viewDetail = (student) => {
  router.push(`/teacher/grade-detail/${selectedExam.value}/${student.studentId}`)
}

// 分页处理
const handleSizeChange = (val) => {
  pageSize.value = val
  // 重新加载数据
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  // 重新加载数据
}

// 监听窗口大小变化
const handleResize = () => {
  scoreDistChart.value?.resize()
  questionTypeChart.value?.resize()
}

onMounted(() => {
  initScoreDistChart()
  initQuestionTypeChart()
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  window.removeEventListener('resize', handleResize)
  scoreDistChart.value?.dispose()
  questionTypeChart.value?.dispose()
})
</script>

<style scoped>
.grade-analysis {
  padding: 20px;
}

.stat-cards {
  margin-bottom: 20px;
}

.stat-card {
  text-align: center;
}

.stat-content {
  padding: 20px;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: #409EFF;
  margin-bottom: 10px;
}

.stat-label {
  color: #909399;
}

.chart-row {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style> 